package com.yoki.cloud.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yoki.cloud.common.model.exception.enums.BasicRestStatus;
import com.yoki.cloud.common.model.exception.enums.RestStatus;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author kissy
 * @Description: rest调用通用封装体
 * @date 2017/10/24 下午4:47
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class InvokeResult<T> implements Serializable {

    @Getter
    private int code;
    @Getter
    private StringBuffer msg;
    @Getter
    private T data;

    public InvokeResult setMsg(String msg){
        if(null == this.msg){
            this.msg = new StringBuffer();
        }
        this.msg.append(msg);
        return this;
    }

    public InvokeResult setMsg(StringBuffer msg){
        if(null != msg) {
            return setMsg(msg.toString());
        }
        return this;
    }

    public String getMsg() {
        if(null != this.msg){
            return this.msg.toString();
        }
        return "";
    }

    public InvokeResult appendMsg(String msg){
        if(null == this.msg){
            this.msg = new StringBuffer();
        }
        this.msg.append(msg);
        return this;
    }

    public InvokeResult appendMsg(StringBuffer msg){
        if(null == this.msg){
            this.msg = new StringBuffer();
        }
        this.msg.append(msg);
        return this;
    }

    public static InvokeResult ok() {
        InvokeResult r = new InvokeResult();
        r.setMsg(BasicRestStatus.OK.message())
                .setCode(BasicRestStatus.OK.code());
        return r;
    }

    public static InvokeResult ok(Object data) {
        InvokeResult r = new InvokeResult();
        r.setData(data)
                .setMsg(BasicRestStatus.OK.message())
                .setCode(BasicRestStatus.OK.code());
        return r;
    }

    public static InvokeResult failure(String msg) {
        InvokeResult r = new InvokeResult();
        r.setMsg(msg)
                .setCode(BasicRestStatus.BUSINESS_ERROR.code());
        return r;
    }

    public static InvokeResult failure(RestStatus status) {
        InvokeResult r = new InvokeResult();
        r.setMsg(status.message())
                .setCode(status.code());
        return r;
    }

    public static InvokeResult failure(RestStatus status , String appendMsg) {
        InvokeResult r = new InvokeResult();
        r.setMsg(status.message() + appendMsg)
                .setCode(status.code());
        return r;
    }

    public static InvokeResult error() {
        InvokeResult r = new InvokeResult();
        r.setCode(BasicRestStatus.SERVER_INTERNAL_ERROR.code())
                .setMsg(BasicRestStatus.SERVER_INTERNAL_ERROR.message());
        return r;
    }

    public static InvokeResult passNoData() {
        InvokeResult r = new InvokeResult();
        r.setCode(BasicRestStatus.OK_NO_DATA.code())
                .setMsg(BasicRestStatus.OK_NO_DATA.message());
        return r;
    }

}
